$(document).ready(function(e){ 
	createPageTab();
	createTaskListTab();
	findTaskByPage(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var taskList = null;
var curPageNum = null;
function findTaskByPage(pageNow){
	var tmp2=root+"/admin/task!findTaskByPage";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			//创建表格头信息
			if(taskList!=null) createTaskListTab();
			totalPageNum = json.totalPageNum;
			taskList = json.pageData;
			curPageNum = json.curPageNum;
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				//htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				//htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].content+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].startDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].endDate.replace("T"," ")+'</div></td>';
				//htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				//htmlStr += '<div align="center" class="STYLE2 STYLE1"><a href="'+root+'/admin/product!goProductList?task.id='+json.pageData[i].id+'">品牌商品</a></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdateTaskDiv('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="deleteCompany('+json.pageData[i].id+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findTaskByPage(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findTaskByPage((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findTaskByPage((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findTaskByPage(json.totalPageNum);});
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findTaskByPage(p);
	}
}

function showAddTaskDiv(){
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAddTaskDiv(){
	$("#addTaskForm")[0].reset();
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function saveTask(){
	if($("#taskContent").val()==""){
		alert("任务描述不能为空！");
		return;
	}
	if($("#taskStartDate").val()==""){
		alert("起始时间不能为空！");
		return;
	}
	if($("#taskEndDate").val()==""){
		alert("结束时间不能为空！");
		return;
	}
	$("#addTaskForm").submit();
}

function showUpdateTaskDiv(pageNow, index){
	$("#backGroud").show();
	$("#updateDiv").show();
	$("#taskId_u").val(taskList[index].id);
	$("#taskContent_u").val(taskList[index].content);
	var s = taskList[index].startDate;
	var e = taskList[index].endDate;
	$("#taskStartDate_u").val(s.substring(0,10));
	$("#taskEndDate_u").val(e.substring(0,10));
	$("#updateTaskButton").unbind("click");
	$("#updateTaskButton").bind("click",function(){updateTask(pageNow);});
}

function closeUpdateTaskDiv(){
	$("#taskId_u").val("");
	$("#taskContent_u").val("");
	
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function updateTask(pageNow){
	if($("#taskContent_u").val()==""){
		alert("任务描述不能为空！");
		return;
	}
	if($("#taskStartDate_u").val()==""){
		alert("起始时间不能为空！");
		return;
	}
	if($("#taskEndDate_u").val()==""){
		alert("结束时间不能为空！");
		return;
	}
	var tmp2=root+"/admin/task!updateTask";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"task.id":$("#taskId_u").val(), "task.content": $("#taskContent_u").val(), "task.startDate":$("#taskStartDate_u").val(), "task.endDate":$("#taskEndDate_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdateTaskDiv();
			if(json.code==0){
				findTaskByPage(pageNow);
			}else{
				alert("修改失败，请稍后重试！");
			}
		}
	})
}

/**
 * 删除品牌
 * @param {Object} taskID
 * @param {Object} pageNow
 */
function deleteCompany(taskID){
	var ids = "";
	if(taskID==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个任务！");
			return;
		}
	}
	
	var tmp2=root+"/admin/task!deleteTask";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"task.id":taskID, "ids":ids, "page.curPageNum":curPageNum, "isDesc": isDesc},
		dataType:"json",
		success: function(json){
			findTaskByPage(json.curPageNum);
		}
	})
}